package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.ChapterInfo;
import com.ruoyi.business.domain.User;
import com.ruoyi.business.domain.bo.ChapterInfoBo;
import com.ruoyi.business.domain.vo.ChapterInfoVo;
import com.ruoyi.business.domain.vo.CharpterTree;
import com.ruoyi.business.domain.vo.UserVo;
import com.ruoyi.business.mapper.ChapterInfoMapper;
import com.ruoyi.business.service.IChapterInfoService;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.vo.InnerObjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/31
 **/
@SuppressWarnings("ALL")
@Service
public class ChapterInfoServiceImpl extends ServiceImpl<ChapterInfoMapper, ChapterInfo> implements IChapterInfoService {

    @Autowired
    private ChapterInfoMapper chapterInfoMapper;


    @Override
    public TableDataInfo getChapterInfoList(ChapterInfoBo bo) {
        Page<ChapterInfo> page = chapterInfoMapper.selectPage(PageUtils.buildPage(bo), buildQueryWrapper(bo));
        List<ChapterInfoVo> list = page.getRecords().stream().map(item -> {
            ChapterInfoVo vo = new ChapterInfoVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        Page<ChapterInfoVo> target = PageUtils.convert(page, list);
        return PageUtils.buildTableDataInfo(target);
    }

    @Override
    public String insertChapterInfo(ChapterInfoBo bo) {
        ChapterInfo chapterInfo = new ChapterInfo();
        BeanUtils.copyProperties(bo, chapterInfo);
        // 设置新增时间
        chapterInfo.setCreateTime(new Date());
        // 设置小节数
        chapterInfo.setVideoNumber(0L);
        if (null == chapterInfo.getLessonNumber()) {
            // 设置当前的章节数
            Long lessonNumber = chapterInfoMapper.selectMaxLesson(bo.getCourseId());
            if (null == lessonNumber) {
                lessonNumber = 0L;
            }
            chapterInfo.setLessonNumber(lessonNumber + 1);
        }
        chapterInfoMapper.insert(chapterInfo);
        return chapterInfo.getId();
    }

    @Override
    public Boolean updateChapterInfo(ChapterInfoBo bo) {
        ChapterInfo chapterInfo = chapterInfoMapper.selectById(bo.getId());
        if (null != chapterInfo) {
            BeanUtils.copyBeanProp(chapterInfo, bo, false);
            chapterInfo.setUpdateTime(new Date());
            return chapterInfoMapper.updateById(chapterInfo) == 1;
        }
        return false;
    }

    @Override
    public Boolean deleteChapterInfo(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 如果需要校验，逻辑写入此if
        }
        return removeByIds(ids);
    }

    @Override
    @Transactional
    public Boolean moveDown(String id) {
        ChapterInfo chapterInfo = chapterInfoMapper.selectById(id);
        if (null != chapterInfo) {
            // 查询下一个id
            ChapterInfo next = chapterInfoMapper.selectNextId(chapterInfo.getLessonNumber(), chapterInfo.getCourseId());
            if (null != next) {
                move(chapterInfo, next);
            }else {
                throw new RuntimeException("已经是最后一个了哦");
            }
        }
        return true;
    }

    @Override
    public List<InnerObjVo> queryChapterInfoRef(String courseId) {
        return chapterInfoMapper.queryChapterInfoRef(courseId);
    }

    @Override
    @Transactional
    public Boolean moveUp(String id) {
        ChapterInfo chapterInfo = chapterInfoMapper.selectById(id);
        if (null != chapterInfo) {
            // 查询下一个id
            ChapterInfo pre = chapterInfoMapper.selectPreId(chapterInfo.getLessonNumber(), chapterInfo.getCourseId());
            if (null != pre) {
                move(chapterInfo, pre);
            }else {
                throw new RuntimeException("这是第一个不能上移的哦，不能调皮哦!");
            }
        }
        return true;
    }

    public void move(ChapterInfo c1, ChapterInfo c2) {
        Long temp = c1.getLessonNumber();
        c1.setLessonNumber(c2.getLessonNumber());
        c2.setLessonNumber(temp);
        chapterInfoMapper.updateById(c1);
        chapterInfoMapper.updateById(c2);
    }

    private LambdaQueryWrapper buildQueryWrapper(ChapterInfoBo bo) {
        LambdaQueryWrapper<ChapterInfo> wrapper = new LambdaQueryWrapper<ChapterInfo>();
        wrapper.like(StringUtils.isNotBlank(bo.getName()), ChapterInfo::getName, bo.getName());
        if (null != bo.getCourseId()) {
            wrapper.eq(ChapterInfo::getCourseId, bo.getCourseId());
        }
        wrapper.like(StringUtils.isNotBlank(bo.getInfo()), ChapterInfo::getInfo, bo.getInfo());
        wrapper.eq(null != bo.getLessonNumber(), ChapterInfo::getLessonNumber, bo.getLessonNumber());
        wrapper.orderBy(true, true, ChapterInfo::getLessonNumber);
        return wrapper;
    }

    @Override
    public List<CharpterTree> queryChapterTree(Long uploadTeacher) {
        return null;
    }

    @Override
    public List<ChapterInfo> queryChaptersByCourseId(Long courseId) {
        QueryWrapper<ChapterInfo> queryWrapper = new QueryWrapper<>();
        List<ChapterInfo> list = list(queryWrapper);
        return list;
    }
}
